﻿using aier.business.logic;
using aier.dataaccess.model;
using aier.dataaccess.dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using log4net;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 社区
    /// </summary>
    [Authorize]
    [RoutePrefix("api/Community")]
    public class CommunityController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILog log { get; set; }
        /// <summary>
        /// 社区逻辑
        /// </summary>
        public CommunityBll communityBll { get; set; }

        public ApiTool apiTool { get; set; }

        /// <summary>
        /// 获取所有的社区信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetAllCommunity")]
        public IHttpActionResult GetAllCommunity()
        {
            List<CommunityView> communitys = communityBll.QueryAllCommunity();

            return Ok<List<CommunityView>>(communitys);
        }

        /// <summary>
        /// 通过ID获取社区信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetCommunityById")]
        public IHttpActionResult GetCommunityById(string communityId)
        {
            CommunityView communityView = communityBll.QueryCommunityById(communityId);

            return Ok<CommunityView>(communityView);
        }

        /// <summary>
        /// 通过名称获取社区信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetCommunityByName")]
        public IHttpActionResult GetCommunityByName(string province, string city, string county,string communityName)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            List<CommunityView> communityViews = communityBll.GetCommunityByName(province, city, county,communityName, userInfo.siteid.ToString());

            return Ok<List<CommunityView>>(communityViews);
        }
        /// <summary>
        /// 根据不同条件查询社区对象,只要属性不为空就视为查询条件
        /// 查询条件为：社区ID/省份ID/市ID/区ID/CountyID/街道ID/名称(模糊搜索)
        /// 原程序 LocationQueryServiceImpl/queryCommunityByStreet
        /// </summary>
        /// <param name="communityView">社区对象</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("QueryCommunityByStreet")]
        public IHttpActionResult QueryCommunityByStreet(CommunityView communityView)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            communityView.new_hospital = userInfo.siteid;
            List<CommunityView> communityViews = communityBll.QueryCommunityByStreet(communityView);
            return Ok<List<CommunityView>>(communityViews);
        }
        /// <summary>
        /// 根据不同条件查询社区数量,只要属性不为空就视为查询条件
        /// 查询条件为：社区ID/省份ID/市ID/区ID/CountyID/街道ID/名称
        /// 原程序 LocationQueryServiceImpl/queryCommunityCount
        /// </summary>
        /// <param name="communityView">社区对象</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("QueryCommunityCount")]
        public IHttpActionResult QueryCommunityCount(CommunityView communityView)
        {
            int count = communityBll.QueryCommunityCount(communityView);
            return Ok<int>(count);
        }
        /// <summary>
        /// 创建社区
        /// </summary>
        /// <param name="community">社区对像</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Put(Community community)
        {
            return Ok<ResResult>(communityBll.CreateCommuntity(community));
        }

        /// <summary>
        /// 修改社区
        /// </summary>
        /// <param name="community">社区对像</param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult Post(Community community)
        {
           ResResult resResult = communityBll.UpdateCommunityById(community);
            return Ok(resResult);
        }

        /// <summary>
        /// 社区查询信息
        /// 原程序 ：ScreeningServiceImpl->  listCommunity(String name, String province, String county, String contact, String city, String street, String mobile, String ownerid, Integer statecode)
        /// </summary>
        /// <param name="name">社区名称</param>
        /// <param name="province">省ID</param>
        /// <param name="county">区/县ID</param>
        /// <param name="contact">联系人ID</param>
        /// <param name="city">市ID</param>
        /// <param name="street">街道ID</param>
        /// <param name="mobile">手机</param>
        /// <param name="ownerid">负责人</param>
        /// <param name="statecode">状态</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("QueryCommunityByCondition")]
        public IHttpActionResult QueryCommunityByCondition(string name, string province, string county, string contact, string city, string street, string mobile, string ownerid, int statecode)
        {
            try
            {
                var views = communityBll.QueryCommunityByCondition(name, province, county, contact, city, street, mobile, ownerid, statecode);
                return Ok<IEnumerable<CommunityView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 社区查询信息
        /// 原程序 ：ScreeningServiceImpl->  listCommunity(String province, String county, String city, String ownerid)
        /// </summary>
        /// <param name="province">省ID</param>
        /// <param name="county">区/县ID</param>
        /// <param name="city">市ID</param>
        /// <param name="ownerid">负责人</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryCommunityByPosition")]
        public IHttpActionResult QueryCommunityByPosition(string province, string city, string county)
        {
            try
            {
                UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
                var views = communityBll.QueryCommunityByPosition(province, city, county, null, userInfo.siteid.ToString());
                return Ok<IEnumerable<CommunityView>>(views);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }
    }
}
